package com.springboot.frame.comm.render;

import java.io.Writer;
import java.util.Map;
import java.util.Properties;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.velocity.VelocityContext;

/**
 * Velocity模板渲染
 *
 * @author TaoYu
 */
@Data
@AllArgsConstructor
public class VelocityRender implements Render {

  private static final org.apache.velocity.app.VelocityEngine ENGINE = new org.apache.velocity.app.VelocityEngine();

  static {
    Properties p = new Properties();
    p.setProperty("file.resource.loader.class",
        "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    ENGINE.setProperties(p);
    ENGINE.init();
  }

  private String templatePath;

  @Override
  public void process(Map<String, Object> renderData, Writer writer) {
    ENGINE.getTemplate(templatePath).merge(new VelocityContext(renderData), writer);
  }

}
